﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using EShop.Model;
using EShop.Common;

namespace EShop.DAL
{
    public class MerchandiseSellDAL:BaseDAL
    {
        /// <summary>
        /// 取单个上架商品的信息
        /// </summary>
        /// <param name="strBarCode"></param>
        /// <returns></returns>
        public MerchandiseSell GetSigleSellMerchandiseDetail(string strBarCode)
        {
            string strUSP = "USP_Merchandise_Sell_Query";
            try
            {
                MerchandiseSell oMerchandiseSell = null;
                SqlParameter[] parameters = {
                                                new SqlParameter("@BarCode",strBarCode)
                                            };
                SqlDataReader reader = (SqlDataReader)DbHelper.ExecuteReaderSQL(strUSP, CommandType.StoredProcedure, parameters);
                if (reader.Read())
                {
                    oMerchandiseSell = new MerchandiseSell();
                    oMerchandiseSell.Barcode = strBarCode;
                    oMerchandiseSell.MerchandiseName = reader["MerchandiseName"].ToString();
                    try
                    {
                        oMerchandiseSell.StockInOrder = reader.GetInt32(reader.GetOrdinal("StockInOrder"));
                    }
                    catch { }
                    try
                    {
                        oMerchandiseSell.SellQuantity = reader.GetInt32(reader.GetOrdinal("SellQuantity"));
                    }
                    catch { }
                    oMerchandiseSell.SellPrice = reader.GetDecimal(reader.GetOrdinal("SellPrice"));
                    oMerchandiseSell.SalePrice = reader.GetDecimal(reader.GetOrdinal("SalePrice"));
                    oMerchandiseSell.UnitName = reader.GetString(reader.GetOrdinal("UnitName"));
                    try
                    {
                        oMerchandiseSell.SellTime = reader.GetDateTime(reader.GetOrdinal("SellTime"));
                    }
                    catch { }
                }
                reader.Close();

                return oMerchandiseSell;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 取得库存数据，准备上架
        /// </summary>
        /// <returns></returns>
        public DataTable GetStockToSellData()
        {
            string strUSP = "USP_MerchandiseSell_Query";

            try
            {
                return DbHelper.ExecuteDataSetSQL(strUSP, CommandType.StoredProcedure).Tables[0];
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 插入上架数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddMerchandiseSell(MerchandiseSell model)
        {
            string strUSP = "USP_MerchandiseSell_ToSell";

            try
            {
                SqlParameter[] parameters ={
                                           new SqlParameter("@Barcode",model.Barcode),
                                           new SqlParameter("@SellPrice",model.SellPrice),
                                           new SqlParameter("@SalePrice",model.SalePrice)
                                      };
                int iReuslt = DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters);
                if (iReuslt > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }


        /// <summary>
        /// 查询今日所有[指定员工]的销售状况
        /// 
        /// </summary>
        /// <param name="strEmployeeID">被查询的EmployeeID，如果为null则查询今天所有</param>
        /// <returns></returns>
        public DataSet MerchandiseTodaySelled(string strEmployeeID)
        {
            string strUSP = "USP_MerchandiseTodaySelled_Query";
            try
            {
                if (string.IsNullOrEmpty(strEmployeeID))
                {
                    return DbHelper.ExecuteDataSetSQL(strUSP, CommandType.StoredProcedure);
                }
                else
                {
                    SqlParameter parameter = new SqlParameter("@EmployeeID", strEmployeeID);
                    return DbHelper.ExecuteDataSetSQL(strUSP, CommandType.StoredProcedure, parameter);
                }
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

    }
}
